package com.cn.web.controller.ComponentMechanical;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.ComponentMechanical.*;
import com.cn.bean.pojo.bc.User;
import com.cn.config.config.Result;
import com.cn.service.server.ComponentMechanical.RepairApplicationService;
import com.cn.service.server.bc.MaterialServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/Rep")
@Transactional
public class RepairApplicationController {

    private final RepairApplicationService repairApplicationService;
    private final MaterialServer materialServer;

    @Autowired(required = false)
    public RepairApplicationController(MaterialServer materialServer, RepairApplicationService repairApplicationService) {
        this.repairApplicationService = repairApplicationService;
        this.materialServer = materialServer;
    }

    @RequestMapping("/select/OutsourcingResultList")
    @ResponseBody
    public List<RepairApplicationSub> selectOutsourcingResultList(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentName = jsonObject.getString("input1");
        String uniqueCode = jsonObject.getString("input2");
        equipmentName = (equipmentName != null) ? equipmentName : "";
        uniqueCode = (uniqueCode != null) ? uniqueCode : "";
        return repairApplicationService.selectRepairApplicationMain(page, limit, equipmentName, uniqueCode);
    }

    @RequestMapping("/select/RepairApplicationMainAll")
    @ResponseBody
    public List<RepairApplicationMain> selectRepairApplicationMainAll(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String code = jsonObject.getString("inputCode");

        code = (code != null) ? code : "";
        return repairApplicationService.selectRepairApplicationMainAll(page, limit, code);
    }

    @RequestMapping("/select/RepairOutsourcingAll")
    @ResponseBody
    public List<RepairApplicationMain> selectRepairOutsourcingAll(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String code = jsonObject.getString("inputCode");
        code = (code != null) ? code : "";
        return repairApplicationService.selectRepairOutsourcingAll(page, limit, code);
    }

    @RequestMapping("/select/RepairApplicationSub")
    @ResponseBody
    public List<RepairApplicationSub> selectRepairApplicationSub(@RequestBody RepairApplicationSub repairApplicationSub) {
        List<RepairApplicationSub> assists = repairApplicationService.selectRepairSubTable(repairApplicationSub);
        return assists;
    }

    @RequestMapping("/insert/RepairOutsourcing")
    @ResponseBody
    public Result insertRepairOutsourcing(@RequestBody JSONObject jsonObject)  {
        JSONArray dataArray = jsonObject.getJSONArray("childList");
        RepairOutsourcingMainTable mainTable = new RepairOutsourcingMainTable();
        RepairApplicationMain applicationMain = new RepairApplicationMain();
        Integer id =jsonObject.getInteger("id");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String userName = jsonObject.getString("userName");
        Integer repairMode=jsonObject.getInteger("repairMode");
        String supplier=jsonObject.getString("supplier");
        String repairTime=jsonObject.getString("repairTime");
        String repairTimeUnit=jsonObject.getString("repairTimeUnit");
        String supplierAddress=jsonObject.getString("supplierAddress");
        String applicationTime=jsonObject.getString("applicationTime");
        BigDecimal totalAmount=jsonObject.getBigDecimal("totalAmount");
        mainTable.setId(id);
        mainTable.setRepairMode(repairMode);
        mainTable.setRepairTime(repairTime);
        mainTable.setRepairTimeUnit(repairTimeUnit);
        mainTable.setSupplier(supplier);
        mainTable.setSupplierAddress(supplierAddress);
        mainTable.setDjCode(String.valueOf(id));
        mainTable.setReturnTime(format.format(date));
        mainTable.setApplicant(userName);
        mainTable.setApplicationTime(applicationTime);
        mainTable.setTotalAmount(totalAmount);
        mainTable.setDjStatus(0);
        applicationMain.setId(id);
        applicationMain.setReturnTime(format.format(date));
        applicationMain.setTotalAmount(totalAmount);
        applicationMain.setRepairTime(repairTime);
        applicationMain.setRepairTimeUnit(repairTimeUnit);
        int a = repairApplicationService.insertRepairOutsourcingMainTable(mainTable);
        int d=repairApplicationService.updateReturn(applicationMain);
        if (a != 1&&d!=1) {
            return Result.failure("500");
        }
        List<RepairOutsourcingSubTable> statusOperationDtos=repairApplicationService.selectRepairOutsourcingSub(id);
        if (statusOperationDtos.size()>0){
            int sub = repairApplicationService.delRepairOutsourcingSubTable(id);
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<RepairOutsourcingSubTable> djBhList = dataArray.toJavaList(RepairOutsourcingSubTable.class);
            for (RepairOutsourcingSubTable dataStructureDto : djBhList) {
                RepairOutsourcingSubTable subTable = new RepairOutsourcingSubTable();
                RepairApplicationSub repairApplicationSub=new RepairApplicationSub();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = repairApplicationService.getRepairOutsourcingSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                repairApplicationSub.setGrindingCapacity(subTable.getGrindingCapacity());
                repairApplicationSub.setRoughnessStandard(subTable.getRoughnessStandard());
                repairApplicationSub.setExpense(subTable.getExpense());
                repairApplicationSub.setMid(id);
                int b = repairApplicationService.insertRepairOutsourcingSubTable(subTable);
                int c=repairApplicationService.openRepairOutsourcing(subTable.getUniqueCode());
                int e=repairApplicationService.updateReturnSub(repairApplicationSub);
                if (b != 1&&c != 1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success("200","入库成功");
    }

    @RequestMapping("/add/RepairApplicationMain")
    @ResponseBody
    public Result addRepairApplicationMain(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        RepairApplicationMain repairApplicationMain = jsonObject.getObject("formMainTable", RepairApplicationMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        repairApplicationMain.setCreationTime(creationTime);
        repairApplicationMain.setCreator(name);
        repairApplicationMain.setCreatorID(userName);
        JSONArray repairApplicationSubList = jsonObject.getJSONArray("childList");
        List<RepairApplicationSub> grindingPassList = repairApplicationSubList.toJavaList(RepairApplicationSub.class);

        return repairApplicationService.addRepairApplicationMain(repairApplicationMain, grindingPassList);
    }

    @RequestMapping("/delete/RepairApplicationMain")
    @ResponseBody
    public Result deleteRepairApplicationMain(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<Integer> integerList = deleteIdList.stream().map(json -> (Integer) json).collect(Collectors.toList());

        return repairApplicationService.deleteRepairApplicationMain(integerList);
    }

    @RequestMapping("/select/Echo/RepairApplicationMainAll")
    @ResponseBody
    public List<RepairApplicationMain> selectEchoRepairApplicationMainAll(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");

        return repairApplicationService.selectEchoRepairApplicationMainAll(id);
    }

    @RequestMapping("/update/RepairApplicationMain")
    @ResponseBody
    public Result updateRepairApplicationMain(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        RepairApplicationMain repairApplicationMain = jsonObject.getObject("formMainTable", RepairApplicationMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        repairApplicationMain.setModificationTime(creationTime);
        repairApplicationMain.setModifier(name);
        repairApplicationMain.setModifierID(userName);
        JSONArray repairApplicationSubList = jsonObject.getJSONArray("childList");
        List<RepairApplicationSub> grindingPassList = repairApplicationSubList.toJavaList(RepairApplicationSub.class);

        return repairApplicationService.updateRepairApplicationMain(repairApplicationMain, grindingPassList);
    }

    @RequestMapping("/update/RepairApplicationMainAuto")
    @ResponseBody
    public Result updateRepairApplicationMainAuto(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<Integer> integerList = deleteIdList.stream().map(json -> (Integer) json).collect(Collectors.toList());
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        return repairApplicationService.updateRepairApplicationMainAuto(integerList,name,userName,creationTime);
    }
    //审核委外结果
    @RequestMapping("/audit/RepairOutsourcing")
    @ResponseBody
    public Result RepairOutsourcingAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                repairApplicationService.updateApplicationAudit(id);
                repairApplicationService.updateRepairOutsourcingAudit(id);
                List<RepairOutsourcingSubTable> statusOperationDtos=repairApplicationService.selectRepairOutsourcingSub(id);
                repairApplicationService.completeRepairOutsourcing(statusOperationDtos.get(0).getUniqueCode());
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
}
